<?php

namespace app\api\behavior;

use app\common\library\sms\SmsFactory;
use think\Exception;
use app\common\library\Auth;
use think\Log;

class SmsSend
{
    public function run(&$params)
    {
        if (request()->isPost()) {
            $smsType = model('StoreConfig')->getConfig('sms_type', 'value', Auth::instance()->getStoreId());
            if (!$smsType) {
                throw new Exception('平台未设置短信！');
            }
            
            if ($smsType == 1) {
                $targetType = 'Chuanglan';
                $account = model('StoreConfig')->getConfig('sms_chuanglan_account', 'value', Auth::instance()->getStoreId());
                $password = model('StoreConfig')->getConfig('sms_chuanglan_password', 'value', Auth::instance()->getStoreId());
                $sign = model('StoreConfig')->getConfig('sms_chuanglan_sign', 'value', Auth::instance()->getStoreId());
                $options = [
                    'account'   => $account,
                    'password'  => $password,
                    'sign'      => $sign,
                ];
            } else {
                throw new Exception('短信类型不存在！');
            }
            
            $sms = SmsFactory::create($targetType, $options);
            // TODO: 判断不同短信，发送不同的文案
            $bool = $sms->sendSMS($params['mobile'], '短信验证码：' . $params['code']. ',请不要泄漏');
            if (!$bool) {
                Log::write($sms->getErrorMsg(), Log::ERROR);
            }
            return $bool;
        }
    }
}
